package com.xawl.controller;

import com.github.pagehelper.PageHelper;
import com.xawl.pojo.User;
import com.xawl.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Zoom
 * @desc 用户管理接口
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    /* 获取数据列表 */
    @ResponseBody
    @RequestMapping("/getList")
    public JSONObject list1(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<User> allList = userService.queryAll();
        Integer total = userService.getTotal();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", 1);
        jsonObject.put("count", total);
        jsonObject.put("data", allList);
        return jsonObject;
    }

    /* 增加 */
    @RequestMapping("/add")
    public String addUserTest() {
        return "red-user-manage/red-user-manage-edit";
    }

    @RequestMapping("/addData")
    @ResponseBody
    public String addUser(@RequestBody User user) {
        User user1 = new User();
        User user2 = userService.queryUserByName(user.getUsername());
        if (user2 != null) {
            return "error,用户名已存在";
        }
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setFace(user.getFace());
        user1.setType(user.getType());
        System.out.println(user);
        int i = userService.addUser(user);
        return i != 0 ? "ok" : "error";
    }

    @RequestMapping("/edit/{userId}")
    public String editUser(Model model, @PathVariable("userId") int userId) {
        model.addAttribute("userId", userId);
        return "red-user-manage/red-user-manage-edit";
    }

    /* 删除 */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteUser(@RequestBody int id) {
        userService.deleteUser(id);
        return "ok";
    }

    /* 删除多条记录 */
    @RequestMapping("/deletes")
    @ResponseBody
    public String deleteUsers(@RequestBody Long[] ids) {
        int i = userService.deleteUsers(ids);
        return i != 0 ? "ok" : "error";
    }

    /* 修改 */
    @RequestMapping("/update")
    @ResponseBody
    public String updateUser(@RequestBody User user, HttpServletRequest req) {
        int i = userService.updateUser(user);
        return i != 0 ? "ok" : "error";
    }

    /* 查看 */
    @RequestMapping("/view/{userId}")
    public String viewTest(Model model, @PathVariable("userId") int userId) {
        model.addAttribute("userId", userId);
        return "red-user-manage/red-user-manage-view";
    }

    /* 数据回显 */
    @RequestMapping("/viewData")
    @ResponseBody
    public User viewUser(@RequestBody int id, HttpServletRequest request) {
        User user = userService.findUserById(id);
        request.setAttribute("user", user);
        return user;
    }

    // 搜索用户
    @RequestMapping("/searchUser")
    @ResponseBody
    public String searchUser(@RequestParam("username") String username, HttpServletRequest req) {
        String result;
        List<User> userList;
        if (username == null || username.equals("") || username == "") {
            result = "error";
            req.getSession().setAttribute("msg", "未查到");
            return result;
        } else {
            User user = userService.queryUserByName(username);
            userList = new ArrayList<User>();
            userList.add(user);
            result = "ok";
            req.getSession().removeAttribute("msg");
            //结果返回
            req.getSession().setAttribute("userList", userList);
        }
        return result;
    }

}
